<template>
  <div class="container">
    <el-dialog
      :title="curId?'修改文章':'新增文章'"
      :visible="showArticleDialog"
      class="dialog"
      @close="closeDialog"
      :model="roleForm"
      @open="articleView"
    >
      <el-form
        :model="roleForm"
        class="demo-ruleForm"
        :rules="rules"
        ref="ruleForms"
      >
        <el-form-item label="文章标题:" prop="title" label-width="100px">
          <el-input
            placeholder="请输入文章内容"
            v-model="roleForm.title"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="文章内容:"
          class="quill-editor"
          prop="articleBody"
          label-width="100px"
        >
          <quill-editor
            class="editor"
            ref="myQuillEditor"
            :options="editorOption"
            v-model="roleForm.articleBody"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址:" prop="videoURL" label-width="100px">
          <el-input
            placeholder="请输入视频地址"
            v-model="roleForm.videoURL"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="btnOk">确 认</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/articles'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  [{ list: 'ordered' }, { list: 'bullet' }],
  ['blockquote', 'code-block'],
  ['image', 'link']
]
export default {
  props: {
    showArticleDialog: {
      type: Boolean
    },
    curId: {
      type: Number
    }
  },
  data () {
    return {
      roleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOption: {
        modules: {
          toolbar: toolbarOptions
        },
        placeholder: ''
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeDialog () {
      this.$emit('close-dialog')
      this.roleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.ruleForms.resetFields()
    },
    async articleView () {
      if (this.curId) {
        const res = await detail({ id: this.curId })
        this.roleForm = res.data
      }
    },
    loadList () {
      this.$emit('load-list')
    },
    btnOk () {
      this.$refs.ruleForms.validate(async (vaild) => {
        if (vaild) {
          if (this.curId) {
            await update({ ...this.roleForm, id: this.curId })
            this.closeDialog()
            this.loadList()
            this.$message({
              type: 'success',
              message: '编辑角色成功!'
            })
          } else {
            const res = await add(this.roleForm)
            console.log(res)
            this.closeDialog()
            this.loadList()
            this.$message({
              type: 'success',
              message: '新增角色成功!'
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-dialog {
  margin-top: 15vh;
  width: 800px;
}
::v-deep .el-form-item__content {
  margin-left: 100px;
  vertical-align: top;
}
::v-deep .demo-ruleForm .el-form-item__label {
  text-align: center;
  line-height: 32px;
}
::v-deep .ql-container {
  height: 200px;
}
::v-deep .quill-editor .ql-toolbar {
  height: 34px;
  padding: 0;
  padding-left: 8px;
}
.dialog-footer {
  text-align: right;
  padding: 0;
}
</style>
